package com.itHsuSE.houserent.view;

import com.itHsuSE.houserent.domain.House;
import com.itHsuSE.houserent.service.HouseService;
import com.itHsuSE.houserent.utils.Utility;

public class HouseView {
    private boolean loop=true;//控制显示菜单
    private char key=' ';//接受用户选择
    private HouseService houseService=new HouseService(10);

    public void deleteHouse(){
        System.out.println("===================删除房屋===================");
        System.out.println("请输入要删除的房屋编号(-1退出)：");
        int delId=Utility.readInt();
        if(delId==-1){
            System.out.println("===================放弃删除房屋===================");
            return;
        }
        char choise=Utility.readConfirmSelection();
        if(choise=='Y'){
            if(houseService.del(delId)){
                System.out.println("===================删除房屋成功===================");
            }else {
                System.out.println("===================房屋编号不存在，删除失败===================");
            }
        }else {
            System.out.println("===================放弃删除房屋===================");
        }
    }

    public void addHouse(){
        System.out.println("===================添加房屋===================");
        System.out.print("姓名：");
        String name=Utility.readString(8);
        System.out.print("电话：");
        String phone=Utility.readString(12);
        System.out.print("地址：");
        String address=Utility.readString(16);
        System.out.print("月租：");
        int rent=Utility.readInt();
        System.out.print("状态：");
        String state=Utility.readString(3);
        House newHouse=new House(0,name,phone,address,rent,state);
        if(houseService.add(newHouse)){
            System.out.println("=================添加房屋成功=================");
        }else {
            System.out.println("=================添加房屋失败=================");
        }
    }

    public void listHouses(){
        System.out.println("===================房屋列表===================");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(已出租/未出租)");
        House[] houses=houseService.list();
        for (int i = 0; i < houses.length; i++) {
            if(houses[i]!=null){
                System.out.println(houses[i]);
            }
        }
        System.out.println("=================房屋列表显示完毕=================");
    }

    //显示主菜单
    public void mainMenu(){
        do{
            System.out.println("\n===================房屋出租系统===================");
            System.out.println("\t\t\t1 新增房屋");
            System.out.println("\t\t\t2 查找房屋");
            System.out.println("\t\t\t3 删除房屋信息");
            System.out.println("\t\t\t4 修改房屋信息");
            System.out.println("\t\t\t5 显示房屋信息");
            System.out.println("\t\t\t6 退出系统");
            System.out.print("请输入你的选择(1-6):");
            key= Utility.readChar();
            switch (key){
                case '1':
                    addHouse();
                    break;
                case '2':
                    break;
                case '3':
                    break;
                case '4':
                    break;
                case '5':
                    listHouses();
                    break;
                case '6':
                    loop=false;
                    break;
                default:
                    break;
            }
        }while (loop);
    }
}
